<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-03
 * Time: 15:37
 */

namespace App\Http\Controllers\Index;



use App\Base\BaseFunc;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class IndexController extends Controller
{
    public function index()
    {
        $data['goodsData'] = DB::table('re_drug_business')
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('business','business_id','=','re_business_id')
            ->get();
        return view("/index.index",$data);
    }

    public function goodsShop($business_id)
    {
        $data['goodsData'] = DB::table('re_drug_business')
            ->where('business_id',$business_id)
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('business','business_id','=','re_business_id')
            ->get();
        $data['goodsShop'] = DB::table('business')
            ->where('business_id',$business_id)
            ->first();
        $data['check']=DB::table('re_user_business')
            ->where("business_id",$business_id)
            ->where('user_id',session('user_id'))
            ->get();
        //dd($data);
        return view("/index.goodsShop",$data);
    }

    public function userLogin()
    {
        return view("/index.login");
    }

    public function _userLogin(BaseFunc $baseFunc)
    {
        $input = Input::only('user_phone');
        $password = Input::only('user_password');
        $input['user_password'] = md5($password['user_password']);
        $user = DB::table('user')
            ->where('user_phone',"=",$input['user_phone'])
            ->value('user_password');
        if($user == NULL)
        {
            $baseFunc ->setRedirectMessage(false,"您还没有注册，请先注册",NULL,"/register");
        }
        else
        {
            $userData = DB::table('user')
                ->where('user_phone',"=",$input['user_phone'])
                ->where('user_password',"=",$input['user_password'])
                ->first();
            //dd($userData);
            if($user == NULL)
            {
                $baseFunc ->setRedirectMessage(false,"密码错误",NULL,"/login");
            }
            else
            {
                session(['user_id' => $userData->user_id]); //获取登录客户的ID
                session(['user_name' => $userData->user_name]); //获取登录客户的名字
                session(['user_phone' => $userData->user_phone]); //获取登录客户的电话

                $baseFunc ->setRedirectMessage(true,"登录成功",NULL,"/index");
            }
        }
    }

    public function userLogout(BaseFunc $baseFunc)
    {
        session(["user_id"=>NULL]);
        $baseFunc ->setRedirectMessage(true,"您已退出登录",NULL,"/index");
    }

    public function userRegister()
    {

        return view("index.register");
    }

    public function _userRegister(BaseFunc $baseFunc)
    {
        $data = Input::only('user_name','user_password','user_phone');
        $password = Input::only('user_password');
        $data['user_password'] = md5($password['user_password']);
        $check = DB::table('user')
            ->where('user_phone',"=",$data['user_phone'])
            ->get();
        if($check == NULL)
        {
            DB::table('user')->insert($data);
            $baseFunc->setRedirectMessage(true,"注册成功，请您登录",NULL,"/login");
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"注册失败，此手机号已被注册",NULL,"/register");
        }
    }

    public function add_goods_car(BaseFunc $baseFunc)
    {
        $input = Input::only('shop_car_goods');
        $input['shop_car_user'] = session('user_id');
        $input['shop_car_time'] = date('Y-m-d H:i:s');
        DB::table('shop_car')->insert($input);
        $baseFunc->setRedirectMessage(true,'收藏成功',NULL,NULL);
        return redirect()->back();
    }

    public function del_goods_car(BaseFunc $baseFunc)
    {
        $input = Input::only('shop_car_goods');
        DB::table('shop_car')
            ->where('shop_car_goods','=',$input['shop_car_goods'])
            ->where('shop_car_user','=',session('user_id'))
            ->delete();
        $baseFunc->setRedirectMessage(true,'取消收藏',NULL,NULL);
        return redirect()->back();
    }

    public function goods_car()
    {
        $data['goodsData'] = DB::table('shop_car')
            ->where('shop_car_user','=',session('user_id'))
            ->leftJoin('goods','goods_id','=','shop_car_goods')
            ->get();
        return view("/index.goodsCar",$data);
    }

    public function searchDrug()
    {
        $input = Input::only('drug_symptom');
        $drug = $input['drug_symptom'];
        $data['goodsData'] = DB::table('re_drug_business')
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('business','business_id','=','re_business_id')
            ->where('drug_symptom', 'like', "%$drug%")
            ->get();
        return view("/index.index",$data);
    }
}